package ulity;
  

	import java.io.FileOutputStream;
import java.util.List;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;


import model.UserGen;
	public class WriteXml {
	  private String configFile;

	  public void setFile(String configFile) {
	    this.configFile = configFile;
	  }

	  public void saveConfig(List<UserGen>users) throws Exception {
	    // Create a XMLOutputFactory
	    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	    // Create XMLEventWriter
	    XMLEventWriter eventWriter = outputFactory
	        .createXMLEventWriter(new FileOutputStream(configFile));
	    // Create a EventFactory
	    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	    XMLEvent end = eventFactory.createDTD("\n");
	    // Create and write Start Tag
	    StartDocument startDocument = eventFactory.createStartDocument();
	    eventWriter.add(startDocument);
        eventWriter.add(end);
        XMLEvent tab = eventFactory.createDTD("\t");
        
	    // Create config open tag
	    StartElement configStartElement = eventFactory.createStartElement("",
	        "", "UserList");
	    eventWriter.add(configStartElement);
	    eventWriter.add(end);
	    
	    
	    
	    for (UserGen user : users) {
	    	StartElement userStartElement = eventFactory.createStartElement("",
			        "", "user");
	    	eventWriter.add(tab);
	    	eventWriter.add(userStartElement);
		    eventWriter.add(end);
		    // Write the different nodes
		    eventWriter.add(tab);
		    createNode(eventWriter, "username", user.getUserName());
		    eventWriter.add(tab);
		    createNode(eventWriter, "pass", user.getPass());
		    eventWriter.add(tab);
		    createNode(eventWriter, "email", user.getEmail());
		    
		    
		    eventWriter.add(tab);
		    eventWriter.add(eventFactory.createEndElement("", "", "user"));
		    eventWriter.add(end);
		      System.out.println(user);
		    }

	    
	    
	    eventWriter.add(eventFactory.createEndElement("", "", "UserList"));
	    eventWriter.add(end);
	    eventWriter.add(eventFactory.createEndDocument());
	    eventWriter.close();
	  }

	  private void createNode(XMLEventWriter eventWriter, String name,
	      String value) throws XMLStreamException {

	    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	    XMLEvent end = eventFactory.createDTD("\n");
	    XMLEvent tab = eventFactory.createDTD("\t");
	    // Create Start node
	    StartElement sElement = eventFactory.createStartElement("", "", name);
	    eventWriter.add(tab);
	    eventWriter.add(sElement);
	    // Create Content
	    Characters characters = eventFactory.createCharacters(value);
	    eventWriter.add(characters);
	    // Create End node
	    EndElement eElement = eventFactory.createEndElement("", "", name);
	    eventWriter.add(eElement);
	    eventWriter.add(end);

	  }

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ParserXml read = new ParserXml();
		
	    List<UserGen> users = ParserXml.readUser("data/user.xml");
		WriteXml configFile = new WriteXml();
	    configFile.setFile("data/user2.xml");
	    try {
	      configFile.saveConfig(users);
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	}

}
